#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from lib.format_time import *
from lib.HandleMysql import *
from lib.switch_code import *
from shop_conn import *
from cate_city_tools import *
from lib.load_data import *
import json
import sys
import re
import os
sys.setdefaultencoding('utf-8')

def set_ppc_conn():    
    conn = HandleMysql('newppc-s.db.58dns.org','58885','user_ppc_ro','0%wjd]YBLG','dbwww58com_newppc')
    conn.connect()
    return conn

def get_400phone_stats(date):
    conn = set_stats_conn()
    ppc_conn = set_ppc_conn()
    stat_table = 't_call_history'
    line_list = []
    
    query = "select auto_num,user_id,cust_name,caller_time,called_time,call_result_desc,start_date,end_date,ani,ani_provice,ani_city,dni,big_code,ext_code,add_date,leng from t_call_history  where  date(add_date)='%s';"%date
    items = ppc_conn.get_results(query)
    for item in items:
        call_id = item[0]
        user_id = item[1]
        cust_name = item[2]
        caller_time = item[3]
        called_time = item[4]
        call_result_desc = item[5]
        start_date = item[6]
        end_date = item[7]
        ani = item[8]
        ani_provice = item[9]
        ani_city = item[10]
        dni = item[11]
        big_code = item[12]
        ext_code = item[13]
        add_date = item[14]
        call_leng = item[15]

        shop_id = 0
        city_id = 0
        city_name = ''
        cate1_id = 0
        cate1_name = ''
        cate2_id = 0
        cate2_name = ''
        if user_id.isdigit():
            q = "select shop_id,city_id,city_name,cate1_id,cate1_name,cate_id,cate_name from shop_info where user_id=%s"%user_id
            its = conn.get_results(q)
            if len(its) > 0:
                shop_id = its[0][0]
                city_id = its[0][1]
                city_name = its[0][2]
                cate1_id = its[0][3]
                cate1_name = its[0][4]
                cate2_id = its[0][5]
                cate2_name = its[0][6]
        
        param = [call_id,shop_id,user_id,cust_name,city_id,city_name,cate1_id,cate1_name,cate2_id,cate2_name,call_leng,caller_time,called_time,call_result_desc,start_date,end_date,ani,ani_provice,ani_city,dni,big_code,ext_code,add_date]
	line_list.append('\t'.join(map(lambda i : str(i), param)))
        
    cont = '\n'.join(line_list)
    path = "/work3/opt/analytic/lh_tmp_data/call/"
    file_name = "call_info_%s.txt"%date.replace('-',"")
    writeTextToFile(path,file_name,"w",cont)
    
    load_data(conn,path+file_name,stat_table,'\t')
    
    conn.close()
    ppc_conn.close() 
    


def main():
   if len(sys.argv)==1:
        start_date=get_yesterday()
        end_date=start_date
   elif len(sys.argv)==2:
        start_date=format_date(sys.argv[1])
        end_date=start_date
   elif len(sys.argv)==3:
        start_date=format_date(sys.argv[1])
        end_date=format_date(sys.argv[2])

   stat_date=start_date
   while stat_date>=start_date and stat_date<=end_date:
        print "The program of the date of %s is beginning at %s " %(stat_date,get_now())

         #process data
        get_400phone_stats( stat_date) 
        print "The program of the date of %s is end at %s " %(stat_date,get_now())
        stat_date=var_date(stat_date)

if __name__=='__main__':
    print 'start'
    main()